package utils

import (
	"codejy/globalfunc"
	"encoding/json"
	"fmt"
	"strings"
)

// 判断是否是json
func IsValidJSON(json1 string) bool {
	if json1 == "" {
		return false
	}
	return json.Valid([]byte(json1))
}

// 判断是否是json数组
func IsValidJSONArray(json1 string) bool {
	validJSON := IsValidJSON(json1)
	if validJSON {
		//判断开头和结尾是否是[]
		if strings.HasPrefix(json1, "[") && strings.HasSuffix(json1, "]") {
			return true
		} else {
			return false
		}
	}
	return false
}

// 判断是否是json对象
func IsValidJSONObject(json1 string) bool {
	validJSON := IsValidJSON(json1)
	if validJSON {
		//判断开头和结尾是否是{}
		if strings.HasPrefix(json1, "{") && strings.HasSuffix(json1, "}") {
			return true
		} else {
			return false
		}
	}
	return false
}

// 接口转string
func InterfaceToString(v interface{}) string {
	//判断值类型
	switch v.(type) {
	case string:
		return v.(string)
	case int:
		return fmt.Sprintf("%d", v.(int))
	case float64:
		//保留2位小数
		v1 := fmt.Sprintf("%.2f", v.(float64))
		//去掉结尾0
		right := strings.TrimRight(v1, "0")
		//判断最后一位是否是.
		if strings.HasSuffix(right, ".") {
			return strings.TrimRight(right, ".")
		} else {
			return right
		}
	case bool:
		return fmt.Sprintf("%t", v.(bool))
	case map[string]interface{}:
		return MapToJson1(v.(map[string]interface{}))
	case []interface{}:
		var str = "["
		for _, v1 := range v.([]interface{}) {
			str += InterfaceToString(v1) + ","
		}
		str = strings.TrimRight(str, ",")
		str += "]"
		return str
	default:
		return fmt.Sprintf("%v", v)
	}
}

// 将{}转换成map
func JsonToMap(s string) map[string]string {
	var obj map[string]interface{}
	err := json.Unmarshal([]byte(s), &obj)
	if err != nil {
		panic(err)
	}
	var maps = make(map[string]string)
	// toMap值都转换成string
	for k, v := range obj {
		maps[k] = InterfaceToString(v)
	}
	return maps
}

// 将[]转换成切片map
func JsonToSliceMap(s string) []map[string]string {
	var obj []map[string]interface{}
	err := json.Unmarshal([]byte(s), &obj)
	if err != nil {
		panic(err)
	}
	var maps = make([]map[string]string, len(obj))
	// toMap值都转换成string
	for i := 0; i < len(obj); i++ {
		maps[i] = make(map[string]string)
		for k, v := range obj[i] {
			maps[i][k] = InterfaceToString(v)
		}
	}
	return maps
}

// 将map转换成json
func MapToJson(m map[string]string) string {
	jsonStr, _ := json.Marshal(m)
	return string(jsonStr)
}
func MapToJson1(m map[string]interface{}) string {
	jsonStr, _ := json.Marshal(m)
	return string(jsonStr)
}

// 将切片map转换成json
func SliceMapToJson(m []map[string]string) string {
	jsonStr, _ := json.Marshal(m)
	return string(jsonStr)
}
func SliceMapToJson1(m []map[string]interface{}) string {
	jsonStr, _ := json.Marshal(m)
	return string(jsonStr)
}

// 将map[string]string转换成json字符串
func JsonToMapStr(value1 map[string]string) string {
	var Str string
	Str = "{"
	//以逗号分隔
	for k, v := range value1 {
		//判断v的类型,如果是字符串那么就用"包起来
		k = "\"" + k + "\""
		//如果v不是数字,布尔值 ,json,那么就用"包起来
		if !globalfunc.ExecuteFunc("IsNumber", v).(bool) && !globalfunc.ExecuteFunc("IsBool", v).(bool) && !IsValidJSON(v) {
			v = "\"" + v + "\""
		}
		Str += k + ":" + v + ","
	}
	Str = Str[:len(Str)-1]
	Str += "}"
	return Str
}

// 解析go语言中默认的 map[ , [map[   字符串
func JsonParseMap(v string) (string, bool) {
	var pd = false
	//判断v开头是否是map[ ,结尾是否是]
	if strings.HasPrefix(v, "\"map[") && strings.HasSuffix(v, "]\"") {
		v = v[5 : len(v)-2]
		pd = true
	}
	if strings.HasPrefix(v, "map[") && strings.HasSuffix(v, "]") {
		v = v[4 : len(v)-1]
		pd = true
	}
	if pd {
		//以空格分隔
		split := strings.Split(v, " ")
		var mapStr = make(map[string]string)
		for _, v := range split {
			//以:分隔
			split1 := strings.Split(v, ":")
			mapStr[split1[0]] = split1[1]
		}
		v = JsonToMapStr(mapStr)
		return v, true
	}
	//判断v开头是否是[map[ ,结尾是否是]]
	if strings.HasPrefix(v, "\"[map[") && strings.HasSuffix(v, "]]\"") {
		v = v[2 : len(v)-2]
		pd = true
	}
	if strings.HasPrefix(v, "[map[") && strings.HasSuffix(v, "]]") {
		v = v[1 : len(v)-1]
		pd = true
	}
	if pd {
		//以空格分隔
		var str = "["
		split := strings.Split(v, "map[")
		for _, v1 := range split {
			if v1 == "" {
				continue
			}
			//去除左右空格
			v1 = strings.TrimSpace(v1)
			parseMap, _ := JsonParseMap("map[" + v1)
			str += parseMap + ","
		}
		str = str[:len(str)-1]
		str += "]"
		return str, true
	}
	return v, false
}
